<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;
use app\web\validate\ManyQueueRule as ManyQueueRuleValidate;
use \app\common\model\ManyQueueRule as ManyQueueRuleModel;

/**
 * 多种排位控制器
 * @package app\index\controller
 */
class ManyQueueRule extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取多种排位列表
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['limit'])) {
                $getData['limit'] = '10';
            }
            $ruleModel = new ManyQueueRuleModel();
            $data = $ruleModel->getList($this->userInfo['store_id']);
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加多重排位
     */
    public function add(){
        $getData = $this->request->post();
        $validate = new ManyQueueRuleValidate();
        if (!$validate->scene('add')->check($getData)) {
            $this->result($validate->getError(), '', 204);
        }
        $getData['user_id'] = $this->userInfo['id'];
        $ruleModel = new ManyQueueRuleModel();
        $result = $ruleModel->saveData($this->userInfo['store_id'],$getData);
        if($result == true){
            $this->result('添加排位成功', '', 200);
        }else{
            $this->result('添加排位失败', '', 204);
        }

    }



    /**
     * 取消多种排位
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            try{
                Db::startTrans();
                //删除多种排位
                Db::name('qt_many_queue_rule')->where('id',$getData['id'])->delete();
                //删除技师排班数据
                Db::name('qt_order_many_queue')->where('many_queue_id',$getData['id'])->delete();
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('删除失败','',204);
            }
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 修改多种排位
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $validate = new ManyQueueRuleValidate();
            if (!$validate->scene('edit')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            try{
                //删除技师排班数据
                Db::name('qt_order_many_queue')->where('many_queue_id',$getData['id'])->delete();
                //插入数据
                $ruleModel = new ManyQueueRuleModel();
                $result = $ruleModel->saveData($this->userInfo['store_id'],$getData);
                if($result == true){
                    $this->result('修改排位成功', '', 200);
                }else{
                    $this->result('修改排位失败', '', 204);
                }
            }catch (Exception $exception){
                $this->result('修改排位失败','',204);
            }
        }
        $this->result('无效的请求', '', 204);
    }






}
